package com.hjqjl.whdemo.feature.feature0.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivityNetWorkBinding;
import com.hjqjl.whdemo.feature.feature0.model.GetImagesModel;
import com.hjqjl.whdemo.feature.feature0.model.SentencesModel;
import com.hjqjl.whdemo.net.ApiUrl;
import com.hjqjl.whdemo.net.BaseObserver;
import com.hjqjl.whdemo.net.api.HttpMethods;
import com.hjqjl.whdemo.net.api.HttpNoSingletonMethods;
import com.hjqjl.whdemo.net.api.HttpService;
import com.hjqjl.whdemo.net.entity.GetImagesEntity;

import java.util.Random;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NetWorkActivity extends BaseActivity {

    public static void start(Context context) {
        Intent starter = new Intent(context, NetWorkActivity.class);
        context.startActivity(starter);
    }

    private ActivityNetWorkBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNetWorkBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.includeToolbar.ivToolbarBack.setVisibility(View.VISIBLE);
        binding.includeToolbar.ivToolbarBack.setOnClickListener(v -> finish());

        binding.btnNetWork.setOnClickListener(v ->
//                HttpMethods.getInstance().getImages("1")
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new BaseObserver<GetImagesModel>(pActivity) {
//                            @Override
//                            public void onSuccess(GetImagesModel getImagesModel) {
//                                binding.tvShow.setText(new Random().nextInt(100) + "==>" + getImagesModel.toString());
//                            }
//                        })
                        HttpService.getInstance().getImages2("1")
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new com.apep.netlibrary.use.BaseObserver<GetImagesEntity>(pActivity) {
                                    @Override
                                    public void onSuccess(GetImagesEntity entity) {
                                        binding.tvShow.setText(new Random().nextInt(100) + "==>" + entity.toString());
                                    }
                                })
        );

        binding.btnNetWorkString.setOnClickListener(v ->
                HttpMethods.getInstance().getImagesString("1")
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(@NonNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NonNull String s) {
                                binding.tvShow.setText(new Random().nextInt(100) + "String==>" + s);
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        }));

        binding.btnNetWorkGet.setOnClickListener(v ->
                HttpMethods.getInstance().getSentences()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<SentencesModel>(pActivity) {
                            @Override
                            public void onSuccess(SentencesModel model) {
                                binding.tvShow.setText(new Random().nextInt(100) + "==>" + model.toString());
                            }
                        }));

        binding.btnNetWorkUrl.setOnClickListener(v ->
                new HttpNoSingletonMethods(ApiUrl.douban_url).getImages("1")
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<GetImagesModel>(pActivity) {
                            @Override
                            public void onSuccess(GetImagesModel getImagesModel) {
                                binding.tvShow.setText(new Random().nextInt(100) + "==>" + getImagesModel.toString());
                            }
                        }));
        binding.btnNetWorkPostJson.setOnClickListener(v -> ToastUtils.showShort("未实现"));
    }
}